<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .wrap {
      width: 375px;
      height: 667px;
      border: 1px solid #000;
      margin: 0 auto;
    }

    .tab {
      height: 50px;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .tab .item {
      flex: 1;
      height: 100%;
      line-height: 50px;
      text-align: center;
    }

    .tab .item.current {
      background: #000;
      color: #fff;
    }

    .page {
      position: relative;
      width: 100%;
      height: 617px;
      border: 1px solid #000;
      font-size: 100px;
    }

    .page .item {
      position: absolute;
      top: 0;
      left: 0;
      display: none;
      width: 100%;
      height: 617px;
      line-height: 617px;
      text-align: center;
    }

    .page .item.active {
      display: block;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="tab J_tab">
      <div class="item current">选项一</div>
      <div class="item">选项二</div>
      <div class="item">选项三</div>
    </div>
    <div class="page J_page">
      <div class="item active">页面一</div>
      <div class="item">页面二</div>
      <div class="item">页面三</div>
    </div>
  </div>
  <script type="text/javascript">
    ;
    (function (doc) {
      var oTab = doc.getElementsByClassName('J_tab')[0]
      var oPage = doc.getElementsByClassName('J_page')[0]
      var tabItems = oTab.getElementsByClassName('item')
      var pageItems = oPage.getElementsByClassName('item')
      var curIdx = 0 //第二种方法
      var init = function () {
        bindEvent()
      }

      function bindEvent() {
        oTab.addEventListener('click', tabClick, false)
      }

      function tabClick(e) {
        var e = e || window.event
        var tar = e.target || e.srcElement
        var className = tar.className
        // console.log(className);
        // if (className === 'item') {
        //   var curIdx = Array.prototype.indexOf.call(tabItems, tar)
        //   for (var i = 0; i < tabItems.length; i++) {
        //     tabItems[i].className = 'item'
        //     pageItems[i].className = 'item'
        //   }
        //   tar.className += ' current'
        //   pageItems[curIdx].className += ' active'
        // }
        if (className === 'item') {
          // 第二种方法
          tabItems[curIdx].className = 'item'
          pageItems[curIdx].className = 'item'
          curIdx = Array.prototype.indexOf.call(tabItems, tar)
          tabItems[curIdx].className += ' current'
          pageItems[curIdx].className += ' active'
        }
      }
      init()
    })(document)
  </script>
</body>

</html>